<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
	public function index()
	{
		
		if($this->input->get('user_id')) {
			$user_id = $this->input->get('user_id');
		}
		else if($this->session->userdata('user_id')) {
			$user_id = $this->session->userdata('user_id');
		}		
		
		$query = $this->db->get_where('users',array('id'=>$user_id));
		if($query->num_rows()) {
			$data['user'] = $query->result();
			
		}
			
		
		$this->load->view('user/index', $data);
	}
	public function save()
	{
		$data['id'] = uniqid('10_',true);
		$data['site_username'] = $this->input->post('site_username');
		$data['site_email'] = $this->input->post('site_email').$this->input->post('site_email_domain');
		$data['site_password'] = md5('kunal');
		$this->db->insert('users',$data);
		echo "inserted";
	}
	
	public function signup()
	{
		$this->load->view('user/signup_page');
	}
	
	public  function ajax_search_json()
	{
		$r["users"][] = array(
		"name"=> "rgrove",
      "location"=> "Portland, OR",
      "followers"=> 55,
      "language"=> "JavaScript",
      "fullname"=> "Ryan Grove",
      "username"=> "rgrove",
      "id"=> "user-1465",
      "repos"=> 28,
      "type"=> "user",
      "pushed"=> "2010-11-06T00:15:08.327Z",
      "score"=> 4.8103123,
      "record"=> null,
      "created"=> "2008-02-28T07:08:51Z");
		echo json_encode($r);
	}
	
	public function login()
	{
		$g = $this->input->post();
		$p = $this->input->post();
		if($this->input->post('user-login-submit'))
		{
			$query = $this->db->get_where('users',array('site_username'=>$p['site_username'],
											'site_password'=>md5($p['site_password'])));
			if(!$query->num_rows())
			{
				$validate = false;
			}
			else
			{
				$this->session->set_userdata('loggedin',TRUE);
				$row = $query->result();
				$user->id = $row[0]->id;
				$this->session->set_userdata('user_id',$user->id);
			}
		}
		else
		{
			$this->load->view('user/login_page',$g);
		}
	}
}